﻿namespace PayPal.AdaptivePayments
{
    using System;
    using System.Web.WebPages.Scope;
    using PayPal.Platform.SDK;

    public static class SimplePreapproval
    {
        public static PreapprovalResponse Execute(
                                            string senderEmail,
                                            DateTime startingDate,
                                            decimal maxTotalAmountForAllPayments,
                                            decimal maxAmountPerPayment,
                                            int maxNumberOfPayments,
                                            DateTime endingDate,
                                            string memo,
                                            string userIp,
                                            string deviceId,
                                            string cancelUrl = "",
                                            string returnUrl = "",
                                            string ipnUrl = "",
                                            string currencyCode = "",
                                            string language = "")
        {
            if (string.IsNullOrEmpty(cancelUrl))
            {
                cancelUrl = Profile.CancelUrl;
            }

            if (string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = Profile.ReturnUrl;
            }

            if (string.IsNullOrEmpty(ipnUrl))
            {
                ipnUrl = Profile.IpnUrl;
            }

            if (string.IsNullOrEmpty(currencyCode))
            {
                currencyCode = Profile.CurrencyCode;
            }

            if (string.IsNullOrEmpty(language))
            {
                language = Profile.Language;
            }

            Validate(
                senderEmail,
                startingDate,
                maxTotalAmountForAllPayments,
                maxAmountPerPayment,
                maxNumberOfPayments,
                endingDate,
                memo,
                userIp,
                deviceId,
                language,
                returnUrl,
                cancelUrl,
                currencyCode);

            PreapprovalResponse preapprovalResponse = null;

            PreapprovalRequest request = new PreapprovalRequest();
            request.cancelUrl = cancelUrl;
            request.returnUrl = returnUrl;

            if (!string.IsNullOrEmpty(ipnUrl))
            {
                request.ipnNotificationUrl = ipnUrl;
            }

            if (!string.IsNullOrEmpty(senderEmail))
            {
                request.senderEmail = senderEmail;
            }

            request.clientDetails = new ClientDetailsType();
            request.clientDetails.applicationId = Profile.FNProfile.ApplicationID;
            request.clientDetails.deviceId = deviceId;
            request.clientDetails.ipAddress = userIp;

            request.currencyCode = currencyCode;
            request.requestEnvelope = new RequestEnvelope();
            request.requestEnvelope.errorLanguage = language;

            request.startingDate = startingDate;
            request.maxTotalAmountOfAllPayments = maxTotalAmountForAllPayments;
            request.maxNumberOfPayments = maxNumberOfPayments;
            request.maxAmountPerPayment = maxAmountPerPayment;
            request.endingDate = endingDate;

            request.memo = memo;

            AdapativePayments ap = new AdapativePayments();
            ap.APIProfile = Profile.FNProfile;
            preapprovalResponse = ap.Preapproval(request);

            if (ap.LastError != null)
            {
                throw ap.LastError;
            }

            return preapprovalResponse;
        }

        private static void Validate(
                                string senderEmail,
                                DateTime startingDate,
                                decimal maxTotalAmountForAllPayments,
                                decimal maxAmountPerPayment,
                                int maxNumberOfPayments,
                                DateTime endingDate,
                                string memo,
                                string userIp,
                                string deviceId,
                                string language,
                                string returnUrl,
                                string cancelUrl,
                                string currencyCode)
        {
            ValidationHelper.ValidateProfile(Profile.FNProfile);
            ValidationHelper.ValidateLanguage(language);
            ValidationHelper.ValidateBaseInput(memo, userIp, deviceId);
            ValidationHelper.ValidateUrl(returnUrl, cancelUrl);
            ValidationHelper.ValidateCurrencyCode(currencyCode);
        }
    }
}
